{% extends "layout.html" %}
{% block content %}
    <div class="x-body layui-anim layui-anim-up">
        <form class="layui-form" lay-filter="form">
            <table class="layui-table">
                <tbody>
                    <tr>
                        <th>通道名称</th>
                        <td colspan="1">
                            <input type="text" id="name" name="name" autocomplete="off" class="layui-input" disabled>
                        </td>
                        <th>协议</th>
                        <td colspan="1">
                            <input type="text" id="protocol" name="protocol" autocomplete="off" class="layui-input" disabled>
                        </td>
                    </tr>
                    <tr id="host_part">
                        <th>主机IP</th>
                        <td>
                            <input type="text" id="host_ip" name="host_ip" autocomplete="off" class="layui-input" disabled>
                        </td>
                        <th>主机端口号</th>
                        <td>
                            <input type="text" id="port" name="port" autocomplete="off" class="layui-input" value="502" disabled>
                        </td>
                    </tr>
                    <tr class="com_part1">
                        <th id="com_label">COM口</th>
                        <td>
                            <select id="com" name="com" lay-filter="com" disabled></select>
                        </td>
                        <th id="band_rate_label">波特率</th>
                        <td>
                            <input type="text" id="band_rate" name="band_rate" required="number" lay-verify="band_rate"
                                   autocomplete="off" class="layui-input" value="9600" disabled>
                        </td>
                    </tr>
                    <tr class="com_part2">
                        <th>奇偶校验</th>
                        <td>
                            <input type="text" id="parity" name="parity" required="number" lay-verify="parity"
                                   autocomplete="off" class="layui-input" value="2" disabled>
                        </td>
                        <th>停止位</th>
                        <td>
                            <input type="text" id="stop" name="stop" required="number" lay-verify="stop"
                                   autocomplete="off" class="layui-input" value="1" disabled>
                        </td>
                    </tr>
                    <tr>
                        <th>报文间隔时间/毫秒</th>
                        <td colspan="3">
                            <input type="text" id="IntervalTime" name="IntervalTime" required="number" lay-verify="IntervalTime"
                                   autocomplete="off" class="layui-input" value="100" disabled>
                        </td>
                    </tr>
                    <tr>
                        <th>功能码</th>
                        <td colspan="1">
                            <input type="text" id="register_type" name="register_type" autocomplete="off" class="layui-input" disabled>
                        </td>
                        <th>slave_id</th>
                        <td colspan="1">
                            <input type="number" id="slave_id" name="slave_id" autocomplete="off" class="layui-input" disabled>
                        </td>
                    </tr>
                    <tr>
                        <th><span class="x-red">*</span>数据类型</th>
                        <td>
                            <select id="data_type" name="data_type" lay-filter="data_type">

                            </select>
                        </td>
                        <th><span class="x-red">*</span>地址</th>
                        <td colspan="1">
                            <input type="number" id="address" name="address" required="required" lay-verify="required"
                                   autocomplete="off" class="layui-input">
                        </td>
                    </tr>
                    <tr>
                        <th><span class="x-red">*</span>数据</th>
                        <td colspan="3">
                            <input type="text" id="data" name="data" required="required" lay-verify="required"
                                   autocomplete="off" class="layui-input">
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" style="text-align: center;">
                            <button id="confirmBtn" class="layui-btn" lay-filter="save" lay-submit="">
                                下发
                            </button>
                        </td>
                        <td colspan="2" style="text-align: center;">
                            <button  type="reset" class="layui-btn">
                                重置
                            </button>
                        </td>
                    </tr>
                </tbody>
            </table>
        </form>
    </div>

    <script>
        var channel_id=GetQueryString('channel_id');
        layui.use(['form','layer'], function() {
            $ = layui.jquery;
            var form = layui.form
                , layer = layui.layer;
            $.ajax({
                url: "../protocol/protocoList4collect",
                type: "POST",
                dataType: "json",
                success: function (data) {
                    $("protocol").empty();
                    $.each(data.data, function (protocol_key, protocol_val) {
                        $("#protocol").append(new Option(protocol_val.protocol, protocol_val.protocol_id));
                    });
                    layui.form.render();
                    $.ajax({
                        url: "../modbus/read_serial_ports",
                        type: "POST",
                        dataType: "json",
                        success: function (data) {
                            $("com").empty();
                            $.each(data.data, function (com_key, com_val) {
                                $("#com").append(new Option(com_val, com_val));
                            });
                            layui.form.render();
                            if (channel_id != null) {
                                data = {'channel_id': channel_id};
                                $.ajax(
                                    {
                                        url: 'readChannel'
                                        , type: 'POST'
                                        , dataType: 'json'
                                        , data: data
                                        , success: function (data) {
                                            var protocol = '';
                                            var register_type = '';
                                            if(data.data[0].register_type==5){
                                                register_type = '写线圈';
                                                $("#data_type").append(new Option("遥信","7"));
                                            }else if (data.data[0].register_type==6){
                                                register_type = '写保持寄存器';
                                                $("#data_type").append(new Option("双字节整数(小端)","1"));
                                                $("#data_type").append(new Option("双字节整数(大端)","2"));
                                            }else if (data.data[0].register_type==15){
                                                register_type = '写多个线圈';
                                                $("#data_type").append(new Option("遥信","7"));
                                                $("#data").attr("placeholder", "逗号分隔，例1,2,3");
                                            }else if (data.data[0].register_type==16){
                                                register_type = '写多个保持寄存器';
                                                $("#data_type").append(new Option("双字节整数(小端)","1"));
                                                $("#data_type").append(new Option("双字节整数(大端)","2"));
                                                $("#data_type").append(new Option("四字节浮点数(小端)","3"));
                                                $("#data_type").append(new Option("四字节浮点数(小端2)","4"));
                                                $("#data_type").append(new Option("四字节浮点数(大端)","5"));
                                                $("#data_type").append(new Option("四字节浮点数(大端2)","6"));
                                                $("#data").attr("placeholder", "逗号分隔，例1,2,3");
                                            }
                                            if(data.data[0].protocol==6){
                                                protocol = 'Modbus TCP';
                                                $("#host_part").show();
                                                $(".com_part1").hide();
                                                $(".com_part2").hide();
                                            }else if (data.data[0].protocol==7){
                                                protocol = 'Modbus RTU';
                                                $("#host_part").hide();
                                                $(".com_part1").show();
                                                $(".com_part2").show();
                                            }
                                            //设置内容
                                            if (data.code == 0) {
                                                channel_id = data.data[0].channel_id;
                                                var filename = "";
                                                var IntervalTime = 100;
                                                if(data.data[0].filename!=null){
                                                    if(data.data[0].filename.split("&").length===2){
                                                        filename = data.data[0].filename.split("&")[0]
                                                        IntervalTime = data.data[0].filename.split("&")[1]
                                                    }
                                                }
                                                form.val("form", {
                                                    name: data.data[0].name,
                                                    dataitem_type: data.data[0].dataitem_type,
                                                    host_ip: data.data[0].host_ip,
                                                    port: data.data[0].port,
                                                    protocol: protocol,
                                                    path: data.data[0].path,
                                                    filename:filename,
                                                    com: data.data[0].com,
                                                    band_rate: data.data[0].band_rate,
                                                    local_ip: data.data[0].com,
                                                    local_port: data.data[0].band_rate,
                                                    parity: data.data[0].parity,
                                                    stop: data.data[0].stop,
                                                    enable: data.data[0].enable == "1",
                                                    slave_id: data.data[0].slave_id,
                                                    register_type: register_type,
                                                    address: data.data[0].address,
                                                    count: data.data[0].count,
                                                    run_cycle: data.data[0].run_cycle,
                                                    IntervalTime:IntervalTime
                                                });
                                                layui.form.render();
                                            }
                                        }
                                    });
                            }
                        }
                    });
                }
            });

            //监听提交
            form.on('submit(save)', function(data){
                $.ajax({
                    url: 'sendModbus', type: 'POST', dataType: 'json', data: data.field, success: function (data) {
                        if (data.code == 0) {
                            // layer.msg('操作成功!');
                            layer.alert("操作成功", {icon: 6}, function () {
                                parent.location.reload();
                                // 获得frame索引
                                var index = parent.layer.getFrameIndex(window.name);
                                //关闭当前frame
                                parent.layer.close(index);
                            });
                        } else {
                            layer.msg(data.msg);
                        }
                    }
                });
                return false;
            });
        });
    </script>
{% endblock %}